package cn.tedu.jsd2210.csmall.product.controller;

import cn.tedu.jsd2210.csmall.product.pojo.dto.AttributeAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.AttributeUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.BrandAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.BrandUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.jsd2210.csmall.product.service.IAttributeService;
import cn.tedu.jsd2210.csmall.product.service.IBrandService;
import cn.tedu.jsd2210.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/attributes")
@Api(tags="05.属性管理模块")
@Validated
public class AttributeController {
    @Autowired
    private IAttributeService attributeService;
    //http://localhost:9080/attributes/add-new
    @ApiOperation("添加属性")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AttributeAddNewDto attributeAddNewDto){
            log.debug("开始处理添加属性的请求,参数:{}",attributeAddNewDto);
        attributeService.addNew(attributeAddNewDto);
            return JsonResult.ok();
    }
    //http://localhost:9080/attributes/{?}/delete
    @ApiOperation("根据ID删除属性")
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        log.debug("开始处理添加属性的请求,参数:{}",id);
        attributeService.deleteById(id);
        return JsonResult.ok();
    }
    //http://localhost:9080/attributes/{?}/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改属性")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name="id",value = "属性ID",required = true,dataType = "long",example = "100")
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id,
                                           @Valid AttributeUpdateDto attributeUpdateDto){
        log.debug("开始处理根据id{}修改属性的请求,新数据:{}",id,attributeUpdateDto);
        attributeService.updateInfoById(attributeUpdateDto,id);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributes/{?}
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查找属性")
    @ApiOperationSupport(order = 410)
    public JsonResult<AttributeStandardVO> getStandardById(
            @PathVariable @Range
                    (min = 1L,message = "请提交有效的ID值!")
                    Long id){
        log.debug("开始处理根据ID:{}查询属性列表的请求",id);
        AttributeStandardVO queryResult = attributeService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }
    
    // http://localhost:9080/attributes
    @ApiOperation("属性列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")

    public JsonResult<List<AttributeListItemVO>> list(){
        List<AttributeListItemVO> list = attributeService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/attributes/{templateId:}
    @ApiOperation("按模板ID查找属性列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/template-id/{templateId:[0-9]+}")

    public JsonResult<List<AttributeListItemVO>> listTemplateId(
            @PathVariable @Range
                    (min = 1L,message = "请提交有效的ID值!")
                    Long templateId){
        List<AttributeListItemVO> list = attributeService.listByTemplateId(templateId);
        return JsonResult.ok(list);
    }

}
